package com.kzd.btgame.main

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.lifecycle.LifecycleOwner
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.ConvertUtils
import com.kzd.btgame.R
import com.kzd.btgame.base.BaseViewModel
import com.kzd.btgame.custom.setOnClickAntiShakeListener
import com.kzd.btgame.custom.setTransitionStatusE
import com.kzd.btgame.databinding.FragmentMainGameBinding
import com.kzd.btgame.jetpack.vm.GameViewModel
import com.kzd.btgame.main.game.*
import com.kzd.btgame.main.game.adapter.GameClassifyAdapter
import com.module.lemlin.app.OwnerStatusBar
import com.module.lemlin.http.Status
import com.module.lemlin.owner.OwnerViewBindingFragment
import com.module.lemlin.owner.ownerViewModel

class MainGameFragment : OwnerViewBindingFragment<FragmentMainGameBinding>() {

    companion object {
        fun newInstance(): MainGameFragment {
            return MainGameFragment()
        }
    }

    private val mGameClassifyAdapter = GameClassifyAdapter()

    private val mViewModel: GameViewModel by lazy {
        ownerViewModel(owner = this)
    }

    override val inflate: (LayoutInflater, ViewGroup?, Boolean) -> FragmentMainGameBinding
        get() = FragmentMainGameBinding::inflate

    override fun initStatusBar(statusBar: OwnerStatusBar) {
        statusBar.run {
            addView = mViewBinding.includeAppSearch2.root
            barColor = R.color.white
        }
        super.initStatusBar(statusBar)
    }

    override fun initView() {

        mViewBinding.includeAppRefresh.rvAppRefreshBody.apply {
            layoutManager = LinearLayoutManager(requireContext())
        }.adapter = mGameClassifyAdapter

        mGameClassifyAdapter.setOnItemClickListener { _, _, position ->
            if (mGameClassifyAdapter.selectedPosition == position)
                return@setOnItemClickListener
            val itemData = mGameClassifyAdapter.data[position]
            mViewModel.selectedClassifyIdLiveData.postValue(
                GameViewModel.GameUpdate(itemData.id)
            )
        }

        mViewBinding.includeAppRefresh.mSmartRefreshLayout.setOnRefreshListener {
            mViewModel.gameClassify()
        }

        //进入搜索
        mViewBinding.includeAppSearch2.tvAppSearchHint.setOnClickAntiShakeListener {
            toHomeSearchActivity()
        }
        //下载管理
        mViewBinding.includeAppSearch2.tvAppSearchDown.setOnClickAntiShakeListener {
            toMineDownloadActivity()
        }
        //消息中心
        mViewBinding.includeAppSearch2.tvAppSearchMsg.setOnClickAntiShakeListener {
            toMineMessageActivity()
        }
    }

    override fun initData() {
        mViewModel.gameClassify()
    }

    override fun initViewMode() {
        mViewModel.gameClassifyLiveData.observe(viewLifecycleOwner, { response ->
            setTransitionStatusE(response,
                mGameClassifyAdapter,
                mViewBinding.includeAppRefresh.mSmartRefreshLayout,
                (false))
            if (response.status != Status.SUCCESS)
                return@observe
            val data = response.data?.data
                ?:return@observe
            mViewBinding.includeAppRefresh.mSmartRefreshLayout.setEnableRefresh(false)
            mViewBinding.includeAppRefresh.root.layoutParams = ConstraintLayout.LayoutParams(
                ConvertUtils.dp2px(75f), ViewGroup.LayoutParams.MATCH_PARENT
            )
            mGameClassifyAdapter.setNewInstance(data)
            mViewModel.selectedClassifyIdLiveData.postValue(
                GameViewModel.GameUpdate(data[0].id)
            )
        })

        mViewModel.selectedClassifyIdLiveData.observe(viewLifecycleOwner, { update ->
            val index = mGameClassifyAdapter.data
                .indexOfLast { it.id == update.type }
            if (index == -1 || index == 0) {
                showFeaturedFragment1()
            } else {
                showListFragment1(update.type)
            }
            mGameClassifyAdapter.selectedPosition = index
        })

        BaseViewModel.userAppTipsLiveData.observe(this as LifecycleOwner, { response ->
            if (response.status != Status.SUCCESS)
                return@observe
            val data = response.data?.data
                ?: return@observe
            mViewBinding.includeAppSearch2.viewAppSearchMsgRead.visibility =
                if (data.msg_rs != 1) View.GONE else View.VISIBLE
        })

    }


    private fun showFeaturedFragment1() {
        childFragmentManager.beginTransaction().apply {
            replace(R.id.flMainGameBody, GameFeaturedFragment.newInstance())
        }.commit()
    }

    private fun showListFragment1(type: String) {
        childFragmentManager.beginTransaction().apply {
            replace(R.id.flMainGameBody, GameListFragment.newInstance(type))
        }.commit()
    }
}